package com.cn.demo.springboot.redis.util;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Aspect
@Component
public class RedisClientUtilAop {


	@Pointcut("execution(* com.cn.demo.springboot.redis.util.IRedisClientUtil.*(String,..)) and @annotation(com.cn.demo.springboot.redis.util.RedisClientUtilAnnotation) ")
	public void controllerMethodPointcut() {
	}


	@Around("controllerMethodPointcut()") 
	public Object Interceptor(ProceedingJoinPoint pjp) throws Throwable {
		MethodSignature signature = (MethodSignature) pjp.getSignature();
		Method method = signature.getMethod();
		Parameter[] parameters = method.getParameters();
		short i = 0;
		Object[] args = pjp.getArgs();
		for (Parameter parameter : parameters) {
			if (parameter.isAnnotationPresent(RedisClientUtilAnnotation.class)) {
				if (StringUtils.isEmpty(args[i])) {
					throw new RuntimeException("key 不能为空！");
				} else {
					args[i] = IRedisClientUtil.PREFIX + args[i];
				}
				break;
			}
			i++;
		}
		Object retVal = pjp.proceed(args);
		return retVal;
	}

}
